package com.book.bookshop.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.book.bookshop.entity.BsUser;
import com.book.bookshop.mapper.BsUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;


@Service
public class BsUserService extends ServiceImpl<BsUserMapper, BsUser> {

    @Autowired
    private BsUserMapper bsUserMapper;
    /**
     * 验证用户存在
     */
    public String checkUser(String userName){
        QueryWrapper<BsUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",userName);
        BsUser bsUser = bsUserMapper.selectOne(queryWrapper);

        if (bsUser == null){
            return "101";//用户不存在
        }else {
            return "102";//用户存在
        }
    }


    public String login(BsUser loginUser, HttpSession httpSession) {
        QueryWrapper<BsUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",loginUser.getUsername());
        BsUser user = bsUserMapper.selectOne(queryWrapper);

        if (user ==null){
            return "101";//用户不存在
        }else if (user.getPassword().equals(loginUser.getPassword())) {
            httpSession.setAttribute("user",user);
            return "100";//用户存在
        }else {
            return "102";//密码错误
        }
    }
}
